
use std::io::{Write, Result};
use std::vec::Vec;


struct ConsoleWriter{
}

impl Write for ConsoleWriter {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        println!("ConsoleWriter-write: {:?}", buf);
        Ok(0)
    }
    fn flush(&mut self) -> Result<()> {
        Ok(())
    }
}

#[derive(Debug)]
struct MultiWriter<W> 
where W: Write
{
    writers: Vec<W>,
}

impl<W> MultiWriter<W> 
where W: Write
{
    fn new()-> Self {
        Self {
            writers: Vec::new(),
        }
    }
    fn add(&mut self, w: W) {
       self.writers.push(w);
    }
    fn remove(&mut self, w: W) {
        self.writers.remove(0);
    }
}
impl<W> Write for MultiWriter<W> 
where W: Write
{
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        self.writers
            .iter_mut()
            .for_each(|w| {
                let _ = w.write(buf);
            });
        println!("MultiWriter-write: {:?}", buf);
        Ok(0)
    }
    fn flush(&mut self) -> Result<()> {
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test_main() -> std::io::Result<()>{
        let mut senders = MultiWriter::new();
        senders.add(ConsoleWriter{});
        senders.add(ConsoleWriter{});
        senders.write("hello".as_bytes())?;
        senders.flush()
    }
}